#pragma once

#include "my_event_loop.h"
#include "comm/my_hook_chain.h"

typedef struct {
    int                         nthreads;
    my_event_loop_data_t        *datas;
    my_thread_base_t            *threads;
    my_hook_chain_t             *init_hook_chain;
    my_hook_chain_t             *quit_hook_chain;
} my_worker_processor_t;


// hook_add must be called before my_worker_processor_start.
// hook_handler(loop_data)
void my_worker_processor_add_init_hook(my_hook_handler_pt hook_handler);

// hook_add must be called before my_worker_processor_start.
// hook_handler(loop_data)
void my_worker_processor_add_quit_hook(my_hook_handler_pt hook_handler);

// thread_data_slot_alloc
int my_worker_processor_thread_data_slot_alloc();

// run in main thread.
void my_worker_processor_start(
    int nthreads,
    int64_t mem_freelist_level,
    int64_t aligned_mem_freelists_level);

void my_worker_processor_stop();

void my_worker_processor_post_event(my_dispatch_event_t *dispatch_event);


extern char *my_worker_thread_tag;

static inline my_bool_t
current_thread_is_worker_thread()
{
    my_thread_base_t *thread_base = current_thread_base();
    if (thread_base == NULL) {
        return MY_FALSE;
    }

    if (thread_base->tag != my_worker_thread_tag) {
        return MY_FALSE;
    }

    return MY_TRUE;
}